﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Dimok;
using Dimok.Data.DataSources;
using Dimok.Web.UI;
using Data.DataSources;

public partial class Controls_ToDo : TableControl {
	Dimok.User.UserObject uox = new Dimok.User.UserObject();
	bool _showSelf;
	public bool ShowSelf { 
		get { return _showSelf || new List<string>(new string[] { "true", "1" }).Contains(Request["ShowSelf"] + ""); }
		set { _showSelf = value; }
	}
	protected string Filter { 
		get {
			List<string> filter = new List<string>() { "IsTask=1" };
			if (!IsPostBack && (Request["Filter"] + "") != "") filter.Add(Request["Filter"] + "");
			else {
				if (ddlIsTask.SelectedValue != "") filter.Add("StatusID=" + ddlIsTask.SelectedValue);
				if (ddlTasks.SelectedValue != "") filter.Add("NoteActionCodeID=" + ddlTasks.SelectedValue);
				if (ddlUsers.SelectedValue == "") filter.Add("DateDueBy='" + Dimok.User.UserObject.CurrentUserName + "'");
				else if (ddlUsers.SelectedValue != "*") filter.Add("DateDueBy='" + Lib.GetUserName(ddlUsers.SelectedValue) + "'");
			}
			return string.Join(" AND ", filter.ToArray());
		} 
	}
	protected string UserName = Dimok.User.UserObject.CurrentUserName;
	protected string UserDomain = Dimok.User.UserObject.CurrentUserDomain;
	protected string UserFullName = Dimok.User.UserObject.CurrentUserPath;
	public string UsixID { get { return GridViewEx.SelectedDataKey == null ? "" : GridViewEx.SelectedDataKey["UsixID"] + ""; } }
	public int CompanyID { get { return GridViewEx.SelectedDataKey == null ? 0 : int.Parse("0"+GridViewEx.SelectedDataKey["CompanyID"]); } }
	public int LoanID { get { return GridViewEx.SelectedDataKey == null ? 0 : int.Parse("0"+GridViewEx.SelectedDataKey["LoanID"]); } }
	private Profiler profileLib;
	private string SortExpression { get {return  ViewState["SortExpression"] + ""; } set { ViewState["SortExpression"] = value; } }

	#region State
	protected override void SetStateValues(Hashtable values) {
		base.SetStateValues(values);
		//FilterQueue = values[ClientID + "FilterQueue"] + "";
	}
	protected override void GetStateValues(Hashtable values) {
		base.GetStateValues(values);
		//values[ClientID + "FilterQueue"] = FilterQueue;
	}
	#endregion

	protected override void OnLoad(EventArgs e) {
		base.OnLoad(e);
		if (!IsPostBack && !ScriptManager.GetCurrent(Page).IsInAsyncPostBack) {
			Cache[ds.CacheKeyDependency] = "Tasks";
			ds.OrderBy.Add("DateDue DESC");
		}
		GridViewEx = grv;
		ObjectDS = ds;
		dsUsersCached.TypeName = typeof(Lib).FullName;
		if (!IsPostBack && !ScriptManager.GetCurrent(Page).IsInAsyncPostBack) {
			//SelectFilter = FilterDefault = "IsTask = 1 AND StatusUser='" + Dimok.User.UserObject.CurrentUserName + "'";
			try {
				ddlFontSize.SelectedValue = profileLib.Page.FontSize + "";
			} catch { }
		}
	}

	void Filter_Changed(object sender, EventArgs e) {
		RefreshNotesCache();
	}

	protected void UpdateNoteStatus(object sender, EventArgs e) {
		Data.SQL.Note.UpdateStatus(sender);
		RefreshNotesCache();
	}

	protected override void OnInit(EventArgs e) {
		base.OnInit(e);
		profileLib = new Profiler(Profile, Page, this);
		ddlFontSize.SelectedIndexChanged += new EventHandler(ddlFontSize_SelectedIndexChanged);
		grv.SearchChanged += new GridViewEx.SearchChangedEventHandler(grv_SearchChanged);
		grv.Sorting += new GridViewSortEventHandler(grv_Sorting);
		grv.RowUpdating += new EventHandler<GridViewUpdateEventArgs>(grv_RowUpdating);
		grv.RowUpdated += new GridViewUpdatedEventHandler(grv_RowUpdated);

		ds.Selecting += new ObjectDataSourceSelectingEventHandler(ds_Selecting);

		ddlTasks.SelectedIndexChanged += new EventHandler(Filter_Changed);
		ddlIsTask.SelectedIndexChanged += new EventHandler(Filter_Changed);
		ddlUsers.SelectedIndexChanged += new EventHandler(Filter_Changed);
		ddlUsers.DataBound += new EventHandler(ddlUsers_DataBound);

	}

	void ddlUsers_DataBound(object sender, EventArgs e) {
		if (!IsPostBack && ShowSelf)
			ddlUsers.SelectedValue = "";
	}

	void ds_Selecting(object sender, ObjectDataSourceSelectingEventArgs e) {
		ds.Where.Add(Filter);
		pnlFilter.InnerHtml = Filter;
		ds.OrderBys = SortExpression;
	}

	void grv_RowUpdated(object sender, GridViewUpdatedEventArgs e) {
		RefreshNotesCache();
		grv.RequiresDataBinding = true;
	}

	void grv_RowUpdating(object sender, GridViewUpdateEventArgs e) {
		e.Keys.Remove("LoanID");
		e.Keys.Remove("UsixID");
		e.Keys.Remove("CompanyID");
	}
	bool RefreshCollection = false;
	void RefreshNotesCache() {
		Cache.Remove(ds.CacheKeyDependency);
		Cache[ds.CacheKeyDependency] = "Tasks";
		grv.DataBound += new EventHandler(grv_DataBound);
		RefreshCollection = true;
	}

	void grv_DataBound(object sender, EventArgs e) {
		if (RefreshCollection) CollectionInfo.UpdateParameters();		
	}

	void grv_Sorting(object sender, GridViewSortEventArgs e) {
		RefreshNotesCache();
		string dir = e.SortDirection == SortDirection.Ascending ? "ASC" : "DESC";
		SortExpression = ds.OrderBys = GridViewEx.SortListClass.Add(e.SortExpression+" "+dir,SortExpression);
				e.Cancel = true;
	}

	void grv_SearchChanged(string search) {
		RefreshNotesCache();
	}

	void ddlFontSize_SelectedIndexChanged(object sender, EventArgs e) {
		profileLib.Page.FontSize = ((ListControl)sender).SelectedValue;
		profileLib.Save();
	}


	protected string ShowApplicantJS(object applID) { return "event.cancelBubble = true;if(!event.ctrlKey)showApplInfo(" + applID + ")"; }
}
